package edu.pku.sei.modeler.figures;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.FreeformLayer;
import org.eclipse.draw2d.FreeformLayout;
import org.eclipse.draw2d.FreeformViewport;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.ScrollPane;
import org.eclipse.draw2d.Shape;
import org.eclipse.draw2d.StackLayout;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Rectangle;

import edu.pku.sei.modeler.uml.ResourceFactory;

public class SubjectFigure extends Shape {
	private static final org.eclipse.swt.graphics.Color USECASE_COLOR = ColorConstants.lightBlue;

	

	private UMLPackageContentFigure contentFigure;


	public SubjectFigure() {
		super();
		XYLayout layout = new XYLayout();
		this.setLayoutManager(layout);

		this.setOpaque(false); // non-transparent figure

		contentFigure = new UMLPackageContentFigure();
		contentFigure.setOpaque(false);
		contentFigure.setForegroundColor(USECASE_COLOR);

		
		//nameLabel.setLabelAlignment(PositionConstants.TOP);
	//	contentFigure.add(nameLabel);

		this.add(contentFigure);
	}

	public IFigure getContentPane() {
	return contentFigure.getContentPane();
}

	
	public void setName(String name) {
		contentFigure.getNameLabel().setText(name);
	}

	@Override
	public void setBounds(Rectangle rect) {
		super.setBounds(rect);
		contentFigure.setBounds(rect);
	//	 makeLabelPositionTop();
	}


	
	

	@Override
	protected void fillShape(Graphics graphics) {
		// TODO Auto-generated method stub
		
	}



	@Override
	protected void outlineShape(Graphics graphics) {
		// TODO Auto-generated method stub
		
	}

	
	class UMLPackageContentFigure extends CommonFigure {
		/** pane where the content of the element is draw */
		protected final IFigure pane;
		private Label nameLabel;
		/**
		 * Creates a new UMLPackageContentFigure.
		 */
		public UMLPackageContentFigure() {
			super();
			// Set content layout
			 setLayoutManager(new StackLayout());

			// Add pane for content
			pane = new FreeformLayer();
			pane.setLayoutManager(new FreeformLayout());
			// Add scrollpane
			ScrollPane scrollpane = new ScrollPane();
			scrollpane.setScrollBarVisibility(ScrollPane.NEVER);
			scrollpane.setViewport(new FreeformViewport());
			scrollpane.setContents(pane);
			
			nameLabel = new Label("subject figure");
			nameLabel.setFont(ResourceFactory.getHeadFont());
			nameLabel.setForegroundColor(ColorConstants.black);
			pane.add(nameLabel);
			
			
			this.add(scrollpane);
			// Set colors
			// setBackgroundColor(headerFigure.getBackgroundColor());
			setBorder(new LineBorder(1));
			setOpaque(false);
		}

		public Label getNameLabel() {
			return nameLabel;
		}

		private void makeLabelPositionTop() {
			Rectangle useCaseArea = this.pane.getBounds();
			int centerX = useCaseArea.x + useCaseArea.width / 2;
			int centerY = useCaseArea.y;

			Dimension labelSize = nameLabel.getPreferredSize();
			nameLabel.setSize(labelSize);
			nameLabel.getBounds().x = centerX - labelSize.width / 2;
			nameLabel.getBounds().y = centerY + 3;
		}
		
		/**
		 * Returns the content pane of this figure
		 * 
		 * @return the content pane of this figure
		 */
		public IFigure getContentPane() {
			return pane;
		}

		/**
		 * {@inheritDoc}
		 */
		@Override
		public void setBounds(Rectangle rect) {
			super.setBounds(rect);
			makeLabelPositionTop();
		}
//		
		@Override
		protected boolean useLocalCoordinates() {
			return true;
		}
		
	}
	


}
